from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from areas.models import Area
from areas.serializers import AreaSerializer, SubAreaSerializer

from rest_framework_extensions.cache.mixins import CacheResponseMixin


class AreaViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    # 区划信息不分页
    pagination_class = None
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()
    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer


class AreaModelViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    pagination_class = None
    # queryset = Area.objects.filter(parent=None)
    def get_queryset(self):
        # action  lisr,retrieve,update,delete,create
        # retrieve获取市区县信息
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
    #
    # serializer_class = AreaSerializer








# class ProvienceAPIView(APIView):
#     def get(self,request):
#         # 1.获取所有省的数据
#         areas = Area.objects.filter(parent=None)
#         # 2.将对象列表转换为字典列表
#         serializer = AreaSerializer(areas,many=True)
#         return Response(serializer.data)